/**  
* @Title: CrUserTransactionRecordService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月22日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.mapper.cityRun.CrShopUserMapper;
import com.lemon.mapper.cityRun.CrUserTransactionRecordMapper;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrUserTransactionRecord;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: CrUserTransactionRecordService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月22日
 * @版本 V 1.0 
 */
@Service
public class CrUserTransactionRecordServiceImpl implements CrUserTransactionRecordService {
	@Resource
	private CrUserTransactionRecordMapper tranMapper;
	@Resource
	private CrShopUserMapper uMapper;

	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param trans
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月22日
	 */
	@Override
	public List<CrUserTransactionRecord> selectListBySelective(CrUserTransactionRecord trans) throws Exception {
		PageHelper.startPage(trans.getPageNum() , trans.getPageSize());
		List<CrUserTransactionRecord> list = tranMapper.selectListBySelective(trans);
		return list;
	}

	/**
	 * <p>
	 * Title: updateShopTranInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tran
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月22日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int updateShopTranInfo(CrUserTransactionRecord tran) throws Exception {
		if (tran.getStatus() == 2) {
			//拒绝 - 修改状态, 并添加一条记录返回金额给用户的
			CrUserTransactionRecord record = tranMapper.selectByPrimaryKey(tran.getId());
			//修改用户账户信息
			CrShopUser u = new CrShopUser();
			u.setId(record.getShopUserId());
			u.setAmount(record.getAmount());  //将金额返回到门店账户里面
			u.setAmountLeft(-record.getAmount());  //已提现金额要剪掉当前失败的金额
			uMapper.updateByPrimaryKeySelective(u);
			//添加一条交易记录
			record.setStatus(1);
			record.setTranType(2);
			record.setType(2);
			record.setSerialNumber(StringUtil.createSerialNoCityRun());  //需要重新生成流水号
			tranMapper.insertSelective(record);
		}
		//通过  - 修改状态
		int row = tranMapper.updateByPrimaryKeySelective(tran);
		return row;
	}

	/**
	 * <p>
	 * Title: insertShopTranInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tran
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月22日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int insertShopTranInfo(CrUserTransactionRecord tran) throws Exception {
		//修改用户账户信息 
		CrShopUser u = new CrShopUser();
		u.setId(tran.getShopUserId());
		u.setAmount(-tran.getAmount());
		u.setAmountLeft(tran.getAmount());
		int selective = uMapper.updateUserAmount(u);
		//添加交易记录
		int i = tranMapper.insertSelective(tran);
		return i;
	}

	/**
	 * <p>
	 * Title: getShopTranProfit
	 * </p>
	 * <p>
	 * Description:(一句话阐述): 获取所有商家可提现 和已提现金额
	 * </p>
	 * 
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月6日
	 */
	@Override
	public Map<String , Object> getShopTranProfit(CrUserTransactionRecord tran) throws Exception {
		PageHelper.startPage(tran.getPageNum() , tran.getPageSize());
		return tranMapper.getShopTranProfit();
	}
}
